home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Files / Select Folder w⁄ Gray files / SFGray.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-07  |  4.4 KB  |  200 lines  |  [TEXT/KAHL]

  1. #include <Packages.h>
  2. #include <Folders.h>
  3.  
  4. /*****
  5.  * a structure used to pre-select a folder
  6.  *****/
  7. typedef struct {
  8.     FSSpec                *theFile;
  9.     StandardFileReply    *theReply;
  10. } MyDataRecord;
  11.  
  12.  
  13. #define isDirectory     0x0010
  14. #define kReportDlgID    257
  15. #define kSFPutDlgID        256
  16.  
  17.  
  18. /*****
  19.  * On first call through CustomGetFile(), set everything so folder specified in 
  20.  * myDataPtr->theFile is preselected
  21.  *****/
  22. pascal short dlgHook(short item, DialogPtr theDialog, MyDataRecord *myDataPtr)
  23. {
  24.     if (item == sfHookFirstCall)
  25.     {
  26.         myDataPtr->theReply->sfFile = *(myDataPtr->theFile);
  27.         
  28.         // the following line ensures the first subfolder in the folder specified
  29.         // is selected.  If we didn't nil out the name, the last subfolder would
  30.         // be selected.
  31.         myDataPtr->theReply->sfFile.name[0] = 0;
  32.         myDataPtr->theReply->sfScript = 0;
  33.         return sfHookChangeSelection;
  34.     }
  35.     return item;
  36. }
  37.  
  38.  
  39. /*****
  40.  * Display a folder at the location specified by myFSSpec.  The files within this
  41.  * folder are grayed out.  We return with a full FSSpec, or -1 if user hit cancel.
  42.  *****/
  43. OSErr GetFileGray(FSSpec *myFSSpec)
  44. {
  45.     OSErr            err;
  46.     
  47.     MyDataRecord    myData;
  48.     
  49.     /*
  50.      * the default name is used in building a FSSpec for the reply.  It must
  51.      * not be passed in as a null string, or your "select this folder" button
  52.      * won't ever be active.
  53.      */
  54.     Str255                defaultName = "\pMust Not Be Null";
  55.     StandardFileReply    reply;
  56.     Point                where = {-1, -1};        // autocenter, System 7!
  57.     short                activeList[2] = {1, 7};    // only keyboard on file list
  58.         
  59.     myData.theFile = myFSSpec;
  60.     myData.theReply = &reply;
  61.     
  62.     CustomPutFile("\p", 
  63.                   defaultName, 
  64.                   &reply, 
  65.                   kSFPutDlgID, 
  66.                   where, 
  67.                   (DlgHookYDProcPtr)dlgHook, 
  68.                   nil, 
  69.                   activeList, 
  70.                   nil, 
  71.                   &myData);
  72.     if (reply.sfGood)
  73.         err = FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, nil, myFSSpec);
  74.     else
  75.         err = -1;        // return some arbitrary error telling us cancel was selected.
  76.     return err;
  77. }
  78.  
  79.  
  80. /***** 
  81.  * Set up a file spec to point to System file in System Folder 
  82.  *****/
  83. OSErr SetUpFile(FSSpec *fileSpec)
  84. {
  85.     OSErr    err;
  86.     short    vRefNum;
  87.     long    dirID;
  88.     
  89.     err = FindFolder(kOnSystemDisk,kSystemFolderType,kDontCreateFolder, &vRefNum,&dirID);
  90.     
  91.     if (err) {
  92.         DebugStr("\pFickle FindFolder failed to find fey folder");
  93.         ExitToShell();
  94.     }
  95.     
  96.     err = FSMakeFSSpec(vRefNum,dirID,"\pSystem",fileSpec);
  97.     
  98.     if (err) {
  99.         DebugStr("\pFaulty FSMakeFSSpec fails in forming FSSpec.");
  100.         ExitToShell();
  101.     }
  102.     return err;
  103.   
  104. }
  105.  
  106. /******
  107.  * Report on what file was selected
  108.  ******/
  109. void Report(FSSpec *theFile)
  110. {
  111.     Str27    vRefNumStr;
  112.     Str27    dirIDStr;
  113.  
  114.     NumToString(theFile->vRefNum, vRefNumStr);
  115.     NumToString(theFile->parID, dirIDStr);
  116.     ParamText(theFile->name, vRefNumStr,  dirIDStr,  "\p");
  117.     Alert(kReportDlgID, nil);
  118. }
  119.  
  120. /******
  121.  * Test Displaying folders only, with files within the folders in gray
  122.  ******/
  123. void TestGetFileGray(void)
  124. {
  125.     OSErr    err;
  126.     FSSpec    theFile;
  127.     
  128.     err = SetUpFile(&theFile);
  129.     if (!err)
  130.     {
  131.         err = GetFileGray(&theFile);
  132.         if (!err)
  133.             Report(&theFile);
  134.     }
  135. }
  136.  
  137. /******
  138.  * if the isDirectory bit is set, return false (to show the directory)
  139.  * if the isDirectory bit is clear, return true (to filter out all files)
  140.  ******/
  141. pascal Boolean MyStandardFileFilter(CInfoPBPtr pb, MyDataRecord *myDataPtr)
  142. {
  143.     return ( !(pb->dirInfo.ioFlAttrib & isDirectory) );
  144. }
  145.  
  146. /*****
  147.  * Handle items hit in our dialog
  148.  *****/
  149. pascal short MyDlgFilter(short item, DialogPtr theDialog, MyDataRecord *myDataPtr)
  150. {
  151.     if (item == sfHookFirstCall)
  152.     {
  153.         myDataPtr->theReply->sfFile = *(myDataPtr->theFile);
  154.         
  155.         // the following line ensures the first subfolder in the folder specified
  156.         // is selected.  If we didn't nil out the name, the last subfolder would
  157.         // be selected.
  158.         myDataPtr->theReply->sfFile.name[0] = 0;
  159.         myDataPtr->theReply->sfScript = 0;
  160.         return sfHookChangeSelection;
  161.     }
  162.     if (item == 10)
  163.         return sfItemOpenButton;
  164.     return item;
  165. }
  166.  
  167. /*****
  168.  * Test Displaying folder only, with no files within the folders.
  169.  *****/
  170. TestGetFileNoGray()
  171. {
  172.     FSSpec    myFSSpec;
  173.     SFTypeList    typeList;
  174.     StandardFileReply reply;
  175.     Point    where = {-1, -1};
  176.     MyDataRecord myData;
  177.  
  178.     myData.theFile = &myFSSpec;
  179.     myData.theReply = &reply;
  180.     
  181.     SetUpFile(&myFSSpec);
  182.     CustomGetFile((FileFilterYDProcPtr)MyStandardFileFilter,
  183.                   -1,
  184.                   typeList,
  185.                   &reply,
  186.                   -6042,
  187.                   where,
  188.                   (DlgHookYDProcPtr)MyDlgFilter,
  189.                   nil,
  190.                   0,
  191.                   nil,
  192.                   &myData);
  193.     if (reply.sfGood)
  194.     {
  195.         FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, nil, &myFSSpec);
  196.         Report(&myFSSpec);
  197.     }
  198. }
  199.  
  200.